<template><div><p>本文档最新版为 <a href="https://learnku.com/docs/laravel/10.x" target="_blank" rel="noopener noreferrer">10.x</a>，旧版本可能放弃维护，推荐阅读最新版！</p>
<h2 id="eloquent-集合" tabindex="-1"><a class="header-anchor" href="#eloquent-集合"><span>Eloquent ： 集合</span></a></h2>
<ul>
<li><a href="#introduction">简介</a></li>
<li><a href="#available-methods">可用的方法</a></li>
<li><a href="#custom-collections">自定义集合</a></li>
</ul>
<h2 id="简介" tabindex="-1"><a class="header-anchor" href="#简介"><span>简介</span></a></h2>
<p>Eloquent 返回的所有结果集都是 <code v-pre>Illuminate\Database\Eloquent\Collection</code> 对象的实例，包括通过 <code v-pre>get</code> 方法检索或通过访问关联关系获取到的结果。 Eloquent 的集合对象继承了 Laravel 的 <a href="https://learnku.com/docs/laravel/6.x/collections" target="_blank" rel="noopener noreferrer">集合基类</a>，因此它自然也继承了数十种能优雅地处理 Eloquent 模型底层数组的方法。</p>
<p>而且，所有的集合都可以作为迭代器，你可以像遍历简单的 PHP 数组一样来遍历它们：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">where</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'active'</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">foreach</span> <span class="token punctuation">(</span><span class="token variable">$users</span> <span class="token keyword">as</span> <span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">echo</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>不过，集合比数组更加强大，它通过更加直观的接口暴露出可链式调用的 map / reduce 等操作。例如，让我们移除所有未激活的用户并收集剩余用户的名字：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name class-name-fully-qualified static-context">App<span class="token punctuation">\</span>User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$names</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">reject</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">active</span> <span class="token operator">===</span> <span class="token constant boolean">false</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span></span>
<span class="line"><span class="token operator">-></span><span class="token function">map</span><span class="token punctuation">(</span><span class="token keyword">function</span> <span class="token punctuation">(</span><span class="token variable">$user</span><span class="token punctuation">)</span> <span class="token punctuation">{</span></span>
<span class="line">    <span class="token keyword">return</span> <span class="token variable">$user</span><span class="token operator">-></span><span class="token property">name</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>注意：大多数 Eloquent 集合方法会返回新的 Eloquent 集合实例，但是 <code v-pre>pluck</code>, <code v-pre>keys</code>, <code v-pre>zip</code>, <code v-pre>collapse</code>, <code v-pre>flatten</code> 和 <code v-pre>flip</code> 方法除外，它们会返回一个 <a href="https://learnku.com/docs/laravel/6.x/collections" target="_blank" rel="noopener noreferrer">集合基类</a> 实例。同样，如果 <code v-pre>map</code> 操作返回的集合不包括任何 Eloquent 模型，那么它会被自动转换成集合基类。</p>
</blockquote>
<h2 id="可用方法" tabindex="-1"><a class="header-anchor" href="#可用方法"><span>可用方法</span></a></h2>
<p>所有 Eloquent 的集合都继承了 <a href="https://learnku.com/docs/laravel/6.x/collections#available-methods" target="_blank" rel="noopener noreferrer">Laravel Collection</a> 对象; 因此, 他们也继承了所有集合基类提供的强大的方法。</p>
<p>另外，<code v-pre>Illuminate\Database\Eloquent\Collection</code> 类提供了一套上层的方法来帮你管理你的模型集合。大多数方法返回 <code v-pre>Illuminate\Database\Eloquent\Collection</code> 实例； 然而，也会有一些方法返回基于 <code v-pre>Illuminate\Support\Collection</code> 类的实例。</p>
<h4 id="contains-key-operator-null-value-null" tabindex="-1"><a class="header-anchor" href="#contains-key-operator-null-value-null"><span><code v-pre>contains($key, $operator = null, $value = null)</code></span></a></h4>
<p><code v-pre>contains</code> 方法可用于判断集合中是否包含指定的模型实例。这个方法接收一个主键或者模型实例：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">contains</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="diff-items" tabindex="-1"><a class="header-anchor" href="#diff-items"><span><code v-pre>diff($items)</code></span></a></h4>
<p><code v-pre>diff</code> 方法返回不在给定集合中的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">diff</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="except-keys" tabindex="-1"><a class="header-anchor" href="#except-keys"><span><code v-pre>except($keys)</code></span></a></h4>
<p><code v-pre>except</code> 方法返回给定主键外的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">except</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="find-key-collection-method-first-collection-method" tabindex="-1"><a class="header-anchor" href="#find-key-collection-method-first-collection-method"><span><code v-pre>find($key)</code> {#collection-method .first-collection-method}</span></a></h4>
<p><code v-pre>find</code> 方法查找给定主键的模型。如果 <code v-pre>$key</code> 是一个模型实例， <code v-pre>find</code> 将会尝试返回与主键匹配的模型。 如果 <code v-pre>$key</code> 是一个关联数组, <code v-pre>find</code> 将会使用 <code v-pre>whereIn()</code> 返回所有匹配 <code v-pre>$keys</code> 的模型:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">all</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$user</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">find</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="fresh-with" tabindex="-1"><a class="header-anchor" href="#fresh-with"><span><code v-pre>fresh($with = [])</code></span></a></h4>
<p><code v-pre>fresh</code> 方法从数据库检索集合中的每个模型的新实例。 此外，任何指定的关系都会被预先加载：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fresh</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">fresh</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="intersect-items" tabindex="-1"><a class="header-anchor" href="#intersect-items"><span><code v-pre>intersect($items)</code></span></a></h4>
<p><code v-pre>intersect</code> 方法返回给定集合中也存在的所有模型:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>User</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">intersect</span><span class="token punctuation">(</span><span class="token class-name static-context">User</span><span class="token operator">::</span><span class="token function">whereIn</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'id'</span><span class="token punctuation">,</span> <span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token operator">-></span><span class="token function">get</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="load-relations" tabindex="-1"><a class="header-anchor" href="#load-relations"><span><code v-pre>load($relations)</code></span></a></h4>
<p><code v-pre>load</code> 方法会提前加载集合中所有模型的指定关系:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">load</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments.author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="loadmissing-relations" tabindex="-1"><a class="header-anchor" href="#loadmissing-relations"><span><code v-pre>loadMissing($relations)</code></span></a></h4>
<p>如果尚未加载关系，<code v-pre>loadMissing</code> 方法将会为集合中的所有模型加载给定的关系</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">loadMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'posts'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">loadMissing</span><span class="token punctuation">(</span><span class="token string single-quoted-string">'comments.author'</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="modelkeys" tabindex="-1"><a class="header-anchor" href="#modelkeys"><span><code v-pre>modelKeys()</code></span></a></h4>
<p><code v-pre>modelKeys</code> 方法返回集合中所有模型的主键：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span><span class="token operator">-></span><span class="token function">modelKeys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token comment">// [1, 2, 3, 4, 5]</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="makevisible-attributes" tabindex="-1"><a class="header-anchor" href="#makevisible-attributes"><span><code v-pre>makeVisible($attributes)</code></span></a></h4>
<p><code v-pre>makeVisible</code> 方法显示集合中的每个模型的指定隐藏属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">makeVisible</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phone_number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="makehidden-attributes" tabindex="-1"><a class="header-anchor" href="#makehidden-attributes"><span><code v-pre>makeHidden($attributes)</code></span></a></h4>
<p><code v-pre>makeHidden</code> 隐藏集合中的每个模型指定属性：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">makeHidden</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token string single-quoted-string">'address'</span><span class="token punctuation">,</span> <span class="token string single-quoted-string">'phone_number'</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="only-keys" tabindex="-1"><a class="header-anchor" href="#only-keys"><span><code v-pre>only($keys)</code></span></a></h4>
<p><code v-pre>only</code> 方法返回给定主键的所有模型：</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">only</span><span class="token punctuation">(</span><span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h4 id="unique-key-null-strict-false" tabindex="-1"><a class="header-anchor" href="#unique-key-null-strict-false"><span><code v-pre>unique($key = null, $strict = false)</code></span></a></h4>
<p><code v-pre>unique</code> 方法返回集合中的所有唯一模型。并且删除与集合中另一个模型有相同主键的同一类型的所有模型</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token variable">$users</span> <span class="token operator">=</span> <span class="token variable">$users</span><span class="token operator">-></span><span class="token function">unique</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line"></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div></div></div><h2 id="自定义集合" tabindex="-1"><a class="header-anchor" href="#自定义集合"><span>自定义集合</span></a></h2>
<p>如果你需要使用自定义 <code v-pre>Collection</code> 对象与自己的扩展方法一起使用， 可以在你的模型上重写 <code v-pre>newCollection</code> 方法:</p>
<div class="language-php line-numbers-mode" data-highlighter="prismjs" data-ext="php" data-title="php"><pre v-pre class="language-php"><code><span class="line"><span class="token php language-php"><span class="token delimiter important">&lt;?php</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">namespace</span> <span class="token package">App</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">App<span class="token punctuation">\</span>CustomCollection</span><span class="token punctuation">;</span></span>
<span class="line"><span class="token keyword">use</span> <span class="token package">Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Model</span><span class="token punctuation">;</span></span>
<span class="line"></span>
<span class="line"><span class="token keyword">class</span> <span class="token class-name-definition class-name">User</span> <span class="token keyword">extends</span> <span class="token class-name">Model</span></span>
<span class="line"><span class="token punctuation">{</span></span>
<span class="line">    <span class="token doc-comment comment">/**</span>
<span class="line">     * 创建一个 Eloquent 集合实例.</span>
<span class="line">     *</span>
<span class="line">     * <span class="token keyword">@param</span>  <span class="token class-name"><span class="token keyword">array</span></span>  <span class="token parameter">$models</span></span>
<span class="line">     * <span class="token keyword">@return</span> <span class="token class-name"><span class="token punctuation">\</span>Illuminate<span class="token punctuation">\</span>Database<span class="token punctuation">\</span>Eloquent<span class="token punctuation">\</span>Collection</span></span>
<span class="line">     */</span></span>
<span class="line">    <span class="token keyword">public</span> <span class="token keyword">function</span> <span class="token function-definition function">newCollection</span><span class="token punctuation">(</span><span class="token keyword type-hint">array</span> <span class="token variable">$models</span> <span class="token operator">=</span> <span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span></span>
<span class="line">    <span class="token punctuation">{</span></span>
<span class="line">        <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">CustomCollection</span><span class="token punctuation">(</span><span class="token variable">$models</span><span class="token punctuation">)</span><span class="token punctuation">;</span></span>
<span class="line">    <span class="token punctuation">}</span></span>
<span class="line"><span class="token punctuation">}</span></span>
<span class="line"></span></span></code></pre>
<div class="line-numbers" aria-hidden="true" style="counter-reset:line-number 0"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>一旦你定义了 <code v-pre>newCollection</code> 方法， 你将随时可以从你的自定义集合中接收一个 <code v-pre>Collection</code> 返回的一个模型实例.。如果要为应用程序的每个模型使用自定义集合，就应该重写所有模型扩展的基类模型中 <code v-pre>newCollection</code> 方法</p>
<blockquote>
<p>本译文仅用于学习和交流目的，转载请务必注明文章译者、出处、和本文链接<br>
我们的翻译工作遵照 <a href="https://learnku.com/docs/guide/cc4.0/6589" target="_blank" rel="noopener noreferrer">CC 协议</a>，如果我们的工作有侵犯到您的权益，请及时联系我们。</p>
</blockquote>
<hr>
<blockquote>
<p>原文地址：<a href="https://learnku.com/docs/laravel/6.x/eloquent-collections/5178" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/elo...</a></p>
<p>译文地址：<a href="https://learnku.com/docs/laravel/6.x/eloquent-collections/5178" target="_blank" rel="noopener noreferrer">https://learnku.com/docs/laravel/6.x/elo...</a></p>
</blockquote>
</div></template>


